package servlet;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;

import java.io.File;
import java.io.IOException;

public class ce {

public String pdf(String pgh)
{
    String a=null;
    File file=new File(pgh);

    try (PDDocument document = PDDocument.load(file)) {

        document.getClass();

        if(!document.isEncrypted()) {
            PDFTextStripperByArea stripper = new PDFTextStripperByArea();
            stripper.setSortByPosition(true);
            PDFTextStripper tStripper = new PDFTextStripper();

            String pdfFileInText = tStripper.getText(document);

            String[] lines = pdfFileInText.split("\\r?\\n");

            for(String line : lines) {
                a+=line;
            }
            System.out.println(a);
        }

    } catch (InvalidPasswordException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }
    return a;
}
    public String pdf2(String pgh)
    {  String a=null;
        File file=new File(pgh);

        try (PDDocument document = PDDocument.load(file)) {
            document.getClass();
            if(!document.isEncrypted()) {
                PDFTextStripperByArea stripper = new PDFTextStripperByArea();
                stripper.setSortByPosition(true);
                PDFTextStripper tStripper = new PDFTextStripper();

                String pdfFileInText = tStripper.getText(document);

                String[] lines = pdfFileInText.split("\\r?\\n");

                for(String line : lines) {
                    a+=line+"\n";
                }
                System.out.println(a);
            }

        } catch (InvalidPasswordException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
        return a;
    }
    public static void main(String[] args) {
    }
}

